package com.prs.crm.report;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public abstract class ReportDateUtils extends AbsUtils{
	private final static int[] MONTH = {31,0,31,30,31,30,31,31,30,31,30,31};
	
	/**
	 * 获取某年某月的天数
	 * */
	public int getDaysByMonth(int year, int month){
		if(year%4==0 && year%100 != 0 || year%400==0)
			if(month == 2) return 29; else return MONTH[month-1];
		else
			if(month == 2) return 28; else return MONTH[month-1];
	}
	/**
	 * 获取某年的天数
	 * */
	public int getDayOfYear(int year){
		if(year%4==0 && year%100 != 0 || year%400==0) return 366; else return 365;
	}
	/**
	 * 统计一年中每月的数量
	 * */
	public Map<Integer, Integer> getMonthByYear(Collection<Date> listDates){
		Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
		for(int i = 1; i <= 12; i++) {
			map.put(i, 0);
		}
		for(Date date : listDates) {
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			String[] month = time.substring(0, time.indexOf(" ")).split("-");
			map.put(Integer.parseInt(month[1]), map.get(Integer.parseInt(month[1]))+1);
		}
		return map;
	}
	/**
	 * 统计某年某月中每周的数量
	 * */
	public Map<Integer, Integer> getDaysWeeks(Collection<Date> listDates, int year, int month) {
		Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
		int days = this.getDaysByMonth(year, month);
		int week = days%7 == 0 ? days/7 : days/7 + 1;
		for(int i = 1; i <= week; i++){
			map.put(i, 0);
		}
		for(Date date : listDates){
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			String[] day = time.substring(0,time.indexOf(" ")).split("-");
			int total = Integer.parseInt(day[2]);
			int w = total%7 == 0 ? total/7 : total/7 + 1 ;
			map.put(w, map.get(w) + 1);
		}
		return map;
	}
	/**
     * @param year --> 年份
     * @return year-01-01
     * */
	protected Date convertToDate(String year){
    	try {
			if(year.trim().length() > 0){
				return SimpleDateFormat.getDateInstance().parse(year + "-01-01");
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
    	return null;
    }
    /**
     * @param year  -->  年份
     * @param month -->  月份
     * @return year-month-01
     * */
    protected Date convertToDate(String year, String month){
    	try {
			if(year.trim().length() > 0 && month.trim().length() > 0){
				return SimpleDateFormat.getDateInstance().parse(year + "-" + month + "-01");
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
    	return null;
    }
    /**
     * @param year   --> 年份
     * @param month  --> 月份
     * @param day    --> 天
     * @return year-month-day
     * */
    protected Date convertToDate(String year, String month, String day){
    	try {
			if(year.trim().length() > 0 && month.trim().length() > 0 && day.trim().length() > 0){
				return SimpleDateFormat.getDateInstance().parse(year + "-" + month + "-" + day);
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
    	return null;
    }
    /**
     * @param year   --> 年份
     * @param month  --> 月份
     * @param day    --> 天
     * @param hour   --> 时
     * @return year-month-day hour:01:01
     * */
    protected Date convertToDate(String year, String month, String day, String hour){
    	try {
			if(year.trim().length() > 0 && month.trim().length() > 0 && day.trim().length() > 0){
				return SimpleDateFormat.getDateInstance().parse(year + "-" + month + "-" + day + " " + hour+":"+"01:01");
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
    	return null;
    }
    /**
     * @param year   --> 年份
     * @param month  --> 月份
     * @param day    --> 天
     * @param hour   --> 时
     * @param mi     --> 分
     * @return year-month-day hour:mi:01
     * */
    protected Date convertToDate(String year, String month, String day, String hour, String mi){
    	try {
			if(year.trim().length() > 0 && month.trim().length() > 0 && day.trim().length() > 0){
				return SimpleDateFormat.getDateInstance().parse(year + "-" + month + "-" + day + " " + hour+":"+ mi + ":01");
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
    	return null;
    }
    /**
     * @param year   --> 年份
     * @param month  --> 月份
     * @param day    --> 天
     * @param hour   --> 时
     * @param mi     --> 分
     * @param sec    --> 秒
     * @return year-month-day hour:mi:sec
     * */
    protected Date convertToDate(String year, String month, String day, String hour, String mi, String sec){
    	try {
			if(year.trim().length() > 0 && month.trim().length() > 0 && day.trim().length() > 0){
				return SimpleDateFormat.getDateInstance().parse(year + "-" + month + "-" + day + " " + hour + ":" + mi + ":" + sec);
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
    	return null;
    }
}
